package cn.hh.controller;

import cn.hh.basic.dto.SmsDto;
import cn.hh.basic.exception.BusinessException;
import cn.hh.basic.service.VerifyCodeService;
import cn.hh.basic.util.AjaxResult;
import cn.hh.basic.util.ExcelUtil;
import com.google.common.io.BaseEncoding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.Map;

/**
 *  接口类: 验证码
 *  1.图形验证码
 *  2.手机验证码
 */
@RestController
@RequestMapping("/verifyCode")
public class verifyCodeController {

    @Autowired
    private VerifyCodeService verifyCodeService;

    /**
     *  图形验证码
     * @param key
     * @return
     */
    @GetMapping("/image/{key}")
    public AjaxResult imageCode(@PathVariable("key") String key){
        try {
            String base64Str = verifyCodeService.getImageCode(key);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取图形验证码失败!!!");
        }
    }

    /**
     *  短信验证码  接收参数: map接收
     */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody Map<String,String > map){
        try {
            verifyCodeService.smsCode(map);
            return AjaxResult.me();
        } catch (BusinessException businessException) {
            businessException.printStackTrace();
            return AjaxResult.me().setMsg(businessException.getMessage()).setSuccess(false);
        }catch (Exception e) {
          e.printStackTrace();
            return AjaxResult.me().setMsg("网络繁忙,请稍后重试");
        }
    }

    /**
     *  微信绑定的验证码
     */
    @PostMapping("/binderSmsCode")
    public AjaxResult binderSmsCode(@RequestBody SmsDto smsDto){
        try {
            verifyCodeService.binderSmsCode(smsDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,"发送失败" + e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙!!!");
        }
    }


}
